﻿package mortal.game.view.yyZiZuan
{
    import Message.Game.*;
    import flash.events.*;
    import mortal.common.global.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.utils.*;
    import mortal.mvc.core.*;
    import mortal.mvc.interfaces.*;

    public class YYZiZuanController extends Controller
    {
        private var _module:YYZiZuanModule;
        private var _isShowIcon:Boolean = false;

        public function YYZiZuanController()
        {
            return;
        }// end function

        private function onInfoUpdate(param1:Object = null) : void
        {
            this.ShowIcon();
            var _loc_2:* = SYYPurpleInfo(param1);
            Cache.instance.YYZiZuan.isMonthGet = Boolean(_loc_2.monthIsGet);
            Cache.instance.YYZiZuan.isYearGet = Boolean(_loc_2.yearIsGet);
            Cache.instance.YYZiZuan.yyZiZuanType = _loc_2.yypurple;
            if (isViewShow)
            {
                this._module.refresh();
            }
            return;
        }// end function

        private function ShowIcon() : void
        {
            var _loc_1:* = !YYZiZuanIcon.hasInstance() || YYZiZuanIcon.instance.isHide;
            if (_loc_1)
            {
                if (ParamsConst.instance.isYYPlan)
                {
                    YYZiZuanIcon.instance.show();
                    this._isShowIcon = true;
                }
            }
            return;
        }// end function

        override protected function initView() : IView
        {
            if (this._module == null)
            {
                this._module = new YYZiZuanModule();
                this._module.addEventListener(WindowEvent.SHOW, this.onShow);
                this._module.addEventListener(WindowEvent.CLOSE, this.onClose);
            }
            return this._module;
        }// end function

        private function onShow(event:WindowEvent) : void
        {
            Dispatcher.addEventListener(EventName.YYZiZuanAply, this.onYYZiZuanAply);
            Dispatcher.addEventListener(EventName.YYZiZuanRequestGift, this.onYYZiZuanRequestGift);
            return;
        }// end function

        private function onClose(event:WindowEvent) : void
        {
            Dispatcher.removeEventListener(EventName.YYZiZuanAply, this.onYYZiZuanAply);
            Dispatcher.removeEventListener(EventName.YYZiZuanRequestGift, this.onYYZiZuanRequestGift);
            return;
        }// end function

        private function onYYZiZuanAply(event:Event) : void
        {
            GameUtil.gotoAplyZiZuan();
            return;
        }// end function

        private function onYYZiZuanRequestGift(event:DataEvent) : void
        {
            GameProxy.YYZiZuan.requestGift(int(event.data));
            return;
        }// end function

        override protected function initServer() : void
        {
            NetDispatcher.addCmdListener(ServerCommand.YYZiZuanInfoUpdate, this.onInfoUpdate);
            Dispatcher.addEventListener(EventName.YYZiZuanShow, this.onYYZiZuanShow);
            Dispatcher.addEventListener(EventName.YYZiZuanReceiveGift, this.onReceiveGift);
            return;
        }// end function

        private function onReceiveGift(event:DataEvent) : void
        {
            if (int(event.data) == 1)
            {
                Cache.instance.YYZiZuan.isMonthGet = true;
            }
            else if (int(event.data) == 2)
            {
                Cache.instance.YYZiZuan.isYearGet = true;
            }
            if (isViewShow)
            {
                this._module.refresh();
            }
            return;
        }// end function

        private function onYYZiZuanShow(event:Event) : void
        {
            if (!isViewShow)
            {
                view.show();
            }
            else
            {
                view.hide();
            }
            return;
        }// end function

    }
}
